package com.example.zxslzw2014_8_11.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public String login(String userName, String password, HttpSession session) {
        //1、校验参数
        //2、校验密码是否正确
        //3、返回响应结果
        System.out.println(userName + " " + password);
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return "用户名或者密码为空";
        }
        if(!"smy".equals(userName) || !"111".equals(password)) {
            return "账号或密码错误";
        }
        session.setAttribute("userName", userName);
        return "登录成功";
    }
}

//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpSession;
//
//@RestController
//@RequestMapping("/user")
//public class UserController {
//    @RequestMapping("/login")
//    public String login(String userName, String password, HttpSession session) {
//        //1、校验参数
//        //2、校验密码是否正确
//        //3、返回响应结果
//        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
//            return "用户名或者密码为空";
//        }
//        if(!"smy".equals(userName) || !"111".equals(password)) {
//            return "账号或密码错误";
//        }
//        session.setAttribute("userName", userName);
//        return "登录成功";
//    }
//}
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.util.ArrayList;
//import java.util.List;
//
//@RestController
//@RequestMapping("/message")
//public class UserController {
//    List<MessageInfo> messageInfos = new ArrayList<>();
//
//    /**
//     * 发布留言
//     */
//    @RequestMapping("/publish")
//    public Boolean publish(MessageInfo messageInfo) {
//        //参数校验
//        if(!StringUtils.hasLength(messageInfo.getFrom())
//                || !StringUtils.hasLength(messageInfo.getTo())
//                || !StringUtils.hasLength(messageInfo.getSay())) {
//            return false;
//        }
//        //存留言
//        messageInfos.add(messageInfo);
//        return true;
//    }
//    /**
//     * 获取留言
//     */
//    @RequestMapping("/getList")
//    public List<MessageInfo> getList() {
//        return messageInfos;
//    }
//}
//
////@RestController
////@RequestMapping("/user")
////public class UserController {
////
////    @RequestMapping("/login")
////    public Boolean login(String userName, String password, HttpSession session) {
////        //参数校验
//////        if(userName == null || userName.length() == 0
//////            || password == null || password.length() == 0) {
//////            return false;
//////        }
////        //Spring MVC 提供了更方便写法
////        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
////            return false;
////        }
////        //判断账户密码是否正确
////        //上面已经做了判空处理，userName不会为空，但下面的写法也是一种习惯
////        if("smy".equals(userName) && "123".equals(password)) {
////            session.setAttribute("userName", userName);
////            return true;
////        }
////        return false;
////    }
////    @RequestMapping("/index")
////    //两种写法
//////    public String getUserName(HttpSession session) {
//////        return (String) session.getAttribute("userName");
//////    }
////    public String getUserName(@SessionAttribute("userName") String userName) {
////        return userName;
////    }
////}
//
////package com.example.zxslzw2014_8_11;
////
////import org.springframework.web.bind.annotation.*;
////
//////@RestController
//////@RequestMapping("user")
//////public class UserController {
//////    @RequestMapping("/test01")
//////    public String test(String name, Integer age) {
//////        return "接收到的参数name：" + name + "  接收到的参数age：" + age;
//////    }
//////
//////
//////    @RequestMapping("test02")
//////    public Object test3(Student student) {
//////        return student.toString();
//////    }
//////
//////    @RequestMapping("test03")
//////    public String getTime(@RequestParam(value = "time", required = false) String newTime) {
//////        return "接收到的参数newTime：" + newTime;
//////    }
//////
//////    @RequestMapping("test04")
//////    public String method(String[] str) {
//////        return Arrays.toString(str);
//////    }
//////
//////    @RequestMapping("test05")
//////    public String method3(@RequestParam List<String> list) {
//////        return "size:" + list.size() + ",list:" + list;
//////    }
//////
//////    @RequestMapping("/test06")
//////    public Object method1(@RequestBody Person person) {
//////        return person.toString();
//////    }
//////
//////    @RequestMapping("/test07/{id}/{name}")
//////    public String method2(@PathVariable Integer id, @PathVariable("name") String userName) {
//////        return "解析参数id:" + id + ",name:" + userName;
//////    }
//////
//////    @RequestMapping("test08")
//////    public String getFile(@RequestPart("file") MultipartFile file) throws IOException {
//////        //获取文件名称
//////        String fileName = file.getOriginalFilename();
//////        //文件上传到指定路径
//////        file.transferTo(new File("C:\\Users\\缘客扫\\Desktop\\图集\\图集\\tmp" + file.getOriginalFilename()));
//////        return "接收到的文件名称为: " + fileName;
//////    }
//////
//////
//////    @RequestMapping("/get")
//////    public String getCookie(HttpServletRequest request, HttpServletResponse response) {
//////        //获取参数
//////        //String name = request.getParameter("name");
//////        //获取所有参数
//////        Cookie[] cookies = request.getCookies();
//////
////////        1、不使用lambda表达式
////////        StringBuilder stringBuilder = new StringBuilder();
////////        if (cookies != null) {
////////            for (Cookie ck : cookies) {
////////                stringBuilder.append(ck.getName() + ":" + ck.getValue());
////////            }
////////        }
////////        return "获取Cookie信息:" + stringBuilder;
//////
////////        //2、使用lambda表达式
//////        if (cookies != null) {
//////            Arrays.stream(cookies).forEach(ck -> System.out.println(ck.getName() + ":" + ck.getValue()));
//////        }
//////        return "获取Cookie："+cookies;
//////    }
//////
//////    @RequestMapping("/get2")
//////    public String getCookie2(@CookieValue("smy") String newName) {
//////        return "从Cookie中获取值，smy:" + newName;
//////    }
//////
//////    @RequestMapping("/set")
//////    public String setSess(HttpServletRequest request) {
//////        //从Cookie中获取到SessionId，根据SessionId获取Session对象，如果没有，就会创建一个
//////        HttpSession session = request.getSession();
//////        session.setAttribute("name", "yuanyiqi");
//////        return "设置Session成功";
//////    }
//////
//////    @RequestMapping("getses1")
//////    public String getsSess(HttpServletRequest request) {
//////        //从cookie中获取到了sessionID, 根据sessionID获取Session对象取Session对象,如果session不存在，不会自动创建
//////        HttpSession session = request.getSession(false);
//////        String userName = (String) session.getAttribute("name");
//////        return "name: " + userName;
//////    }
//////
//////    @RequestMapping("getses2")
//////    public String getSess2(HttpSession session) {
//////        String name = (String)session.getAttribute("name");
//////        return "从Session中获取name: " + name;
//////    }
//////
//////    @RequestMapping("getses3")
//////    public String getSess3(@SessionAttribute("name") String name) {
//////        return "从Session中获取name: " + name;
//////    }
//////
//////    @RequestMapping("getHeader1")
//////    public String getHeader1(HttpServletRequest request) {
//////        String userAgent = request.getHeader("User-Agent");
//////        return "从Header获取信息，userAgent:" + userAgent;
//////    }
//////
//////    @RequestMapping("getHeader2")
//////    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
//////        return "从Header获取信息，userAgent:" + userAgent;
//////    }
//////
//////
//////
//////        @RequestMapping("/index")
//////        public String index() {
//////            return "/index.html";
//////        }
//////
//////}
//////@Controller
//////@RequestMapping("/user")
//////public class UserController {
//////    @RequestMapping("/index")
//////    public String index() {
//////        return "/index.html";
//////    }
//////
//////
//////    @RequestMapping("returnData")
//////    public String returnData() {
//////        return "该方法返回数据";
//////    }
//////
//////    @ResponseBody
//////    @RequestMapping("/reml")
//////    public String returnHtml() {
//////        return "<h1> Hello, HTML</h1>";
//////    }
//////
//////    @ResponseBody
//////    @RequestMapping("/returnJson")
//////    public HashMap<String, String> returnJson() {
//////        HashMap<String, String> map = new HashMap<>();
//////        map.put("key1", "value1");
//////        map.put("key2", "value2");
//////        map.put("key3", "value3");
//////        return map;
//////    }
//////
//////    @ResponseBody
//////    @RequestMapping("/setStatus")
//////    public String setStatus(HttpServletResponse response) {
//////        response.setStatus(418);
//////        return "设置状态码成功";
//////    }
//////
//////    @ResponseBody
//////    @RequestMapping(value = "/setContentType", produces = "application/json;charset=utf-8")
//////    public String returnJson2() {
//////        return "{\"ok\":101}";
//////    }
//////
//////    @ResponseBody
//////    @RequestMapping("/setHeader")
//////    public String setHeader(HttpServletResponse response) {
//////        response.setHeader("MyHeader", "MyHeaderValue");
//////        return "设置成功";
//////    }
////
////
//////    @RestController
//////    @RequestMapping("/calculate")
//////    public class UserController {
//////        @RequestMapping("/sum")
//////        public String  calc(Integer num1, Integer num2) {
//////            int sum = num1 + num2;
//////            return "<h1>计算机计算结果: " + sum + "</h1>";
//////        }
//////    }
////
////    @RestController
////    @RequestMapping("/calculate")
////    public class UserController{
////        @RequestMapping("/sum")
////        public String calculate(Integer num1,Integer num2) {
////            int sum = num1 + num2;
////            return "<h1>后端返回的计算结果：" +sum + "<h1>";
////
////        }
////    }
////
//////}
//////
//////
////////@RestController
////////@RequestMapping("/user")
////////public class UserController {
////////    @RequestMapping("/hello")
////////    public String hello(){
////////        return "hello,仲夏是荔枝味！";
////////    }
////////}
